<!DOCTYPE html>
<html>
<head>
	<script type="text/javascript" src="__SCRIPT__/jquery.js"></script>
	<meta charset="utf-8" />
	<style>
		li{list-style: none; padding: 10px 0;}
		label{display:inline-block; width:70px; text-align: right;}
		input#pay-code,input#sms-code{width:168px; height: 25px;}
		input#send, input#Submit{height: 31px; width: 85px; background: #DCDADA; border: none; color: #000;}
		input#Submit{margin-left: 78px; background-color: #0066d8; color: #fff;}
		input#send.disabled{color:#ccc;}
		#ajax-rs{font-size: 12px;}
		.red{color: red;}
		.green{color: green;}
	</style>
</head>
<body onload="load()">
<ul>
	<li>
		<label for="pay-code">支付密码</label>
		<input type="password" id="pay-code" placeholder="请输入您的支付密码" />
		<span id="ajax-rs" class="red"></span>
	</li>
	<li>
		<label for="pay-code">验证码</label>
		<input type="text" id="sms-code" placeholder="请输入您收到的手机验证码" />
		<input type="button" id="send" value="发送" />
	</li>
	<li>
		<input type="button" id="Submit" value="确定" />
	</li>
</ul>
<script type="text/javascript">
	function load() {
		$.ajax('/Api/ChkPayPwd', {
			type: 'POST',
			dataType:'json',
			success: function(code){
				var tips = $('#ajax-rs');
				if( code == 2 ){
					tips.html('支付密码未设置');
				}else{tips.html('');}
			}
		});
	}
	$('#pay-code').blur(function(){
		var v = $(this).val();
		if(v.length > 0){
			$.ajax('/Api/ChkPayPwd', {
				type: 'POST',
				data: {v: v},
				success: function(data){
					var code = data.code;
					var msg  = data.msg;
					var tips = $('#ajax-rs');
					if( code == 1 ){
						tips.removeClass('red');
						tips.addClass('green');
					}else{
						tips.removeClass('green');
						tips.addClass('red');
					}
					tips.html(msg);
				}
			});
		}
	});

	$('#Submit').click(function(){
		var a = $('#pay-code').val();
		var b = $('#sms-code').val();

		if(a.length <= 0 || b.length <= 0){
			alert('支付密码和验证码不能为空.');
			return ;
		}

		$.post('/Api/CheckPay', {p: a, c: b}, function(data){
			var code = data.code;
			var msg  = data.msg;

			if( code == 0 ){
				alert(msg);
			}else if( code == 1 ){
				var index = parent.layer.getFrameIndex(window.name);
				parent.$('#pay-form').submit();
				parent.layer.close(index);
			}

		});
	});

	$('#send').click(function(){
		var _this = $(this);
		var val   = _this.val();
		countDown(_this, val);

		$.ajax('/Api/SendCode',{
			type: 'GET',
			success: function(msg){
				alert(msg);
			}
		});
	});

	function countDown(o, v){
		var second = {:C('SMS')['GAP']};
	var time   = null;
	o.val(second + '秒');
	o.attr('disabled', 'disabled');
	o.addClass('disabled');
	time = setInterval(function(){
		var t = --second + '秒';
		o.val( t );
		if( second <= 0 ){
			clearInterval(time);
			o.val(v);
			o.removeAttr('disabled');
			o.removeClass('disabled');
		}
	}, 1000);
	}
</script>
</body>
</html>